<script setup lang="ts">
import {Delete, Plus} from '@element-plus/icons-vue'
import {ref, watch} from 'vue'

const props = defineProps<{
  isEditorFullscreen?: boolean,
  answers?: string[],
  analysis?: string,
  score?: number
}>()

// 填空题答案
const fillBlankAnswers = ref([''])
// 添加分值
const score = ref(0)
const analysis = ref('')

// 添加填空
const addBlank = () => {
  fillBlankAnswers.value.push('')
}

// 删除填空
const removeBlank = (index: number) => {
  fillBlankAnswers.value.splice(index, 1)
}

// 获取填空题答案，用于提交
const getAnswers = () => {
  return fillBlankAnswers.value
}

// 获取分值
const getScore = () => {
  return score.value
}

const getAnalysis = () => {
  return analysis.value
}

// 初始化填空题数据
const initFillBlankData = () => {
  if (props.answers && props.answers.length > 0) {
    fillBlankAnswers.value = [...props.answers];
  }
  
  if (props.analysis) {
    analysis.value = props.analysis;
  }
  
  if (props.score !== undefined) {
    score.value = props.score;
  }
}

// 监听数据变化
watch([() => props.answers, () => props.analysis, () => props.score], () => {
  initFillBlankData();
}, { immediate: true });

// 暴露给父组件的方法
defineExpose({
  getAnswers,
  getScore,
  getAnalysis
})
</script>

<template>
  <el-form-item
      prop="fillBlank"
      :class="{ 'hide-when-fullscreen': isEditorFullscreen }"
  >
    <div class="question-container">
      <div class="question-header">
        <div class="header-left">
          <h3 class="section-title">填空题选项<span class="required-asterisk">*</span></h3>
          <span class="blank-count">共 {{ fillBlankAnswers.length }} 个填空</span>
        </div>
        <div>
          <el-button type="primary" :icon="Plus" @click="addBlank">
            添加填空
          </el-button>
        </div>
      </div>

      <div class="blanks-wrapper">
        <div class="score-section">
          <div class="section-header">
            <span class="section-label">分值</span>
            <el-tag size="small" effect="plain" type="info">SCORE</el-tag>
          </div>
          <div class="score-input-wrapper">
            <el-input-number
                v-model="score"
                :min="0"
                :max="100"
                controls-position="right"
                placeholder="分值"
                class="score-input"
            />
          </div>
        </div>

        <span class="section-label">答案</span>
        <div v-for="(_answer, index) in fillBlankAnswers" :key="index" class="blank-item">
          <div class="blank-content">
            <div class="blank-number">
              <el-tag size="large" effect="plain" round type="info">
                填空 #{{ index + 1 }}
              </el-tag>
            </div>
            <el-input
                v-model="fillBlankAnswers[index]"
                placeholder="请输入答案"
                class="blank-input"
            />
            <el-button
                v-if="fillBlankAnswers.length > 1"
                type="danger"
                :icon="Delete"
                circle
                plain
                size="small"
                @click="removeBlank(index)"
            />
          </div>
        </div>
        <span class="section-label">解析</span>
        <el-input v-model="analysis" type="textarea" placeholder="请输入题目解析（可选）"/>
      </div>
    </div>
  </el-form-item>
</template>

<style scoped>
.question-container {
  width: 100%;
  background: var(--el-bg-color);
  border-radius: 8px;
  padding: 20px;
  border: 1px solid var(--el-border-color);
  margin-bottom: 16px;
}

.question-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.section-title {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  position: relative;
  padding-bottom: 8px;
}

.section-title::after {
  content: '';
  position: absolute;
  left: 0;
  bottom: 0;
  width: 24px;
  height: 2px;
  background-color: var(--el-color-primary);
  border-radius: 1px;
}

.required-asterisk {
  color: var(--el-color-danger);
  margin-left: 4px;
}

.blank-count {
  color: var(--el-text-color-secondary);
  font-size: 14px;
}

.blanks-wrapper {
  position: relative;
}

/* 填空题样式 */
.blank-item {
  margin-bottom: 16px;
  background-color: var(--el-fill-color-light);
  border-radius: 6px;
  padding: 12px;
  transition: all 0.3s;
}

.blank-item:hover {
  background-color: var(--el-fill-color);
}

.blank-content {
  display: flex;
  align-items: center;
  gap: 16px;
}

.blank-number {
  flex: 0 0 auto;
}

.blank-input {
  flex: 1;
}

.hide-when-fullscreen {
  display: none !important;
}

/* 添加分值样式 */
.score-section {
  width: 200px;
  margin-bottom: 20px;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.section-label {
  font-size: 14px;
  font-weight: 500;
  color: var(--el-text-color-primary);
  position: relative;
  padding-left: 10px;
}

.section-label::before {
  content: '';
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 3px;
  height: 14px;
  background-color: var(--el-color-primary);
  border-radius: 1.5px;
}

.score-input-wrapper {
  display: flex;
  align-items: flex-start;
}

:deep(.score-input) {
  width: 100%;
}

:deep(.score-input .el-input-number__decrease),
:deep(.score-input .el-input-number__increase) {
  border-radius: 0;
}

:deep(.score-input .el-input__wrapper) {
  padding-right: 30px;
}

@media screen and (max-width: 768px) {
  .blank-content {
    flex-direction: column;
    align-items: flex-start;
  }

  .blank-number {
    margin-bottom: 8px;
  }

  .blank-input {
    width: 100%;
    margin-bottom: 8px;
  }
}
</style> 